function myFlat(arr) {
  return arr.reduce((pre, cur) => {
    return Array.isArray(cur) ? pre.concat(myFlat(cur)) : pre.concat([cur])
  }, [])
}
function myFlat2(arr) {
  while (arr.some(e => Array.isArray(e))) {
    arr = [].concat(...arr)
  }
  return arr
}
// console.log(myFlat2([1, 2, 3, [3, 4, 5, 6], [2]]))


// 支持展开指定层数

function flat(arr, dep) {
  let newArr = []
  if (dep === undefined) dep = Infinity
  for (let i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i]) && dep > 0) {
      newArr = [...newArr, ...flat(arr[i], dep - 1)]
    } else {
      newArr.push(arr[i])
    }
  }
  return newArr
}

let arr = [1, [2, [3, [4]]]]
console.log(arr.flat())